//
// Created by lingzhitech on 25-8-1.
//
#include <gtest/gtest.h>
#include "../material/HandleInput.h"
TEST(ch4, hello) {
    std::cout << "hello" << std::endl;
}

TEST(ch4, test01arithmeticundefined) {
    std::cout << sizeof(short) << std::endl;
    short short_value = 32767;
    short_value++;
    std::cout << short_value << std::endl; //溢出
}

void isoddoreven() {
    int a;
    while (std::cin >> a) {
        if (a % 2) {
            std::cout << "a is odd" << std::endl;
        } else {
            std::cout << "a is even" << std::endl;
        }
    }
}

TEST(ch4, test02exercise0406) {
    handle_input("0 1 2 3 4 5 6", isoddoreven);
}

TEST(ch4, test03dereferencecombineincrement) {
    std::vector<int> v{1, 2, 3, 4, 5};
    auto pbegin = v.begin();
    while (pbegin != v.end() && *pbegin >= 0) {
        std::cout << *pbegin++ << std::endl;
    }
}

TEST(ch4, test04toupperwhile) {
    std::string s = "apple pineapple";
    auto beg = s.begin();
    while (beg != s.end() && !std::isspace(*beg)) {
        *(beg - 1) = std::toupper(*beg++);
    }
    std::cout << s << std::endl;
}

TEST(ch4, test05toupperfor) {
    std::string s = "apple pineapple";

    for (auto beg = s.begin(); beg != s.end() && !std::isspace(*beg); ++beg) {
        *beg = std::toupper(*beg);
    }
    std::cout << s << std::endl;
}

TEST(ch4, test06exercise0421) {
    std::vector<int> v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
    for (auto &elem: v) {
        elem = elem % 2 ? elem : elem * 2;
    }
    for (int v1: v) {
        std::cout << v1 << " ";
    }
}

void conditionaloperator() {
    int grade;
    while (std::cin >> grade) {
        std::string result = (grade > 90) ? "high pass" : (grade > 75) ? "pass" : (grade > 60) ? "low pass" : "fail";
        std::cout << result << std::endl;
    }
}

TEST(ch4, test07exercise0422) {
    handle_input("12 95 12 78 84 34 72 9 23 44 76", conditionaloperator);
}

TEST(ch4, test08exercise0428) {
    std::cout << sizeof(char) << std::endl;
    std::cout << sizeof(short) << std::endl;
    std::cout << sizeof(int) << std::endl;
    std::cout << sizeof(long) << std::endl;
    std::cout << sizeof(long long) << std::endl;
    std::cout << sizeof(float) << std::endl;
    std::cout << sizeof(double) << std::endl;
    std::cout << sizeof(long double) << std::endl;
    std::cout << sizeof(bool) << std::endl;
}

TEST(ch4, test09exercise0429) {
    int x[10];
    int *p = x;
    std::cout << sizeof (x) / sizeof (*x) << std::endl;
    std::cout << sizeof(p) / (sizeof (*p)) << std::endl; //64bit system: 8/4 = 2
}

TEST(ch4, test10exercise0430) {
    std::vector<int> v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    std::vector<int>::size_type cnt = v.size();
    std::vector<int>::size_type i = 0;
    while (i != v.size()) {
        v[i++] = cnt--;
    }
    for (const auto &elem: v) {
        std::cout << elem << std::endl;
    }
}

TEST(ch4, test11) {
    std::cout << sizeof(int) << std::endl;
    std::cout << sizeof(unsigned int) << std::endl;
}

TEST(ch4, test12conversionstoconst) {
    int i;
    const int &j = i;
    const int *p = &i;
    // int &r =  j, *q = p;//Non-const lvalue reference 'r' to type int cannot bind to lvalue of type const int
    //Cannot initialize local variable 'q' of type int * with const int *
}

TEST(ch4, test13fourcast) { {
        int i = 8, j = 3;
        double slope = static_cast<double>(j) / i;
    } {
        const char *pc;
        char *p = const_cast<char *>(pc); // ok: but writing through p is undefined
    } {
        const char *cp;
        // error: static_cast can't cast away const
        // char *q = static_cast<char *>(cp);
        static_cast<std::string>(cp); // ok: converts string literal to string
        // const_cast<std::string>(cp); // error: const_cast only changes constness
    } {
        double *ip;
        char *pc = reinterpret_cast<char *>(ip);
    }
}

TEST(ch4, test14exercise0436) {
    int i = 5;
    double d = 2.2;
    i *= static_cast<int>(d);
    std::cout << i << std::endl;
}

TEST(ch4, test15exercise0437) {
    int i;
    double d;
    const std::string *ps;
    char *pc;
    void *pv;
    // pv = static_cast<void*>(const_cast<std::string*>(ps));
    // i = static_cast<int>(*pc);
    // pv = static_cast<void*>(&d);
    pc = static_cast<char*>(pv);
}


